/**
 * @FileName: ExportLastRewardData.java
 * @Author
 * @Description:
 * @Date 2016年4月13日 下午3:08:26
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service.ds;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;

import com.jfinal.kit.Prop;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.model.SalesPromotion;
import cn.hpclub.server.model.SyncData;
import cn.hpclub.server.service.ds.DateUtils.SyncParams;

public class ExportRewardTextData extends ExportTextDataService{
    private static final String FILE_NAME_PREFIX = "dzgyw_";
    private static final String PATH_DS_YEWU     = "YeWu/";

    public ExportRewardTextData(SyncParams params){
        this.params = params;
        Prop prop = DsPropKit.getProp();
        this.ftpParam = new FtpParam(prop.get("dzgywHost"), prop.getInt("dzgywServerPort"), prop.get("dzgywUserName"),
                prop.get("dzgywPassword"), prop.get("dzgywEncoding"), prop.get("dzgywRemotePath"));
    }

    @Override
    String getSql(SyncParams params){
        String select = SalesPromotion.dao.getSelectStatementForCMCCBussinessDataSynchronization();
        String sqlExceptSelect = SalesPromotion.dao
                .getExceptSelectStatementForCMCCBussinessDataSynchronization(params.start, params.end);
        return String.format("%s %s", select, sqlExceptSelect);
    }

    @Override
    String getFileName(SyncParams params){
        return String.format("%s%s%s", FILE_NAME_PREFIX, params.tag, FILE_EXTENSION);
    }

    @Override
    String getFilePath(String fileName){
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM");
        String dirPath = Common.getRootDir() + PATH_DS_YEWU + dateFormater.format(new Date());
        Common.checkDir(dirPath);
        return dirPath + "/" + fileName;
    }

    @Override
    String convertRecordToString(Record item){
        StringBuffer sb = new StringBuffer();
        sb.append(item.getStr("city"));
        sb.append("$");
        sb.append(item.getStr("area"));
        sb.append("$$");
        sb.append(item.getStr("NumCode"));
        sb.append("$");
        sb.append(item.getStr("DisplayName"));
        sb.append("$");
        sb.append(item.getStr("sitetype"));
        sb.append("$");
        sb.append(item.getStr("order_id"));
        sb.append("$");
        sb.append(item.getStr("moudlecode"));
        sb.append("$");
        sb.append(item.getStr("moudlename"));
        sb.append("$");
        sb.append(item.getStr("business_code"));
        sb.append("$");
        sb.append(item.getStr("tariff"));
        sb.append("$");
        sb.append(String.valueOf(item.getBigDecimal("total_amount").intValue()));
        sb.append("$");
        sb.append(item.getStr("waitermobile"));
        sb.append("$");
        sb.append(item.getStr("waitername"));
        sb.append("$");
        sb.append(item.getStr("phone_no"));
        sb.append("$");
        sb.append(item.getStr("cmcc_date"));
        sb.append(" ");
        sb.append(item.getStr("cmcc_time"));
        sb.append("$");
        sb.append(String.valueOf(item.getBigDecimal("reward").intValue()));
        return sb.toString();
    }

    @Override
    void saveExportResult(SyncParams params, String filePath){
        String digest = DigestUtils.md5Hex(filePath);
        SyncData.dao.saveSyncData(params.tag, filePath, digest, getFileName(params));
    }
}
